package com.shane.components.timeSlider
{
	import com.EventWithParams.EventWithParams;
	import com.greensock.TweenLite;
	import com.greensock.easing.*;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import relationships.timeConversions;
	
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	public class timeSlider extends MovieClip
	{
		private var yMin:Number;
		private var yMax:Number;
		private var xOffset:Number
		private var incriment:int = 12;
		public var leftFallPoint:int;
		public var rightFallPoint:int;
		private var timeConverter:timeConversions = new timeConversions();
		
		public var CHANGE_TIMESPAN:String = "CHANGE_TIMESPAN";
		
		public function timeSlider()
		{
			this.buttonMode = true;
			this.useHandCursor = true;
			thumbRight.txt.mouseEnabled = false;
			thumbLeft.txt.mouseEnabled = false;
			
			yMin = Track.x;
			yMax = Track.x + Track.width;
			
			//addEventListener( Event.ENTER_FRAME, everyFrame);
			thumbRight.addEventListener( MouseEvent.MOUSE_DOWN, md, false, 0, true);
			thumbLeft.addEventListener( MouseEvent.MOUSE_DOWN, md, false, 0, true);
			thumbRight.darkness.visible = false;
			thumbLeft.darkness.visible = false;
			
			thumbLeft.addEventListener(MouseEvent.MOUSE_OVER, moverLeft, false, 0, true);
			thumbLeft.addEventListener(MouseEvent.MOUSE_OUT, moutLeft, false, 0, true);
			
			thumbRight.addEventListener(MouseEvent.MOUSE_OVER, moverRight, false, 0, true);
			thumbRight.addEventListener(MouseEvent.MOUSE_OUT, moutRight, false, 0, true);
			
			leftFallPoint = Math.round(thumbLeft.x /this.incriment)*incriment;
			thumbLeft.txt.text = timeConverter.getTag(leftFallPoint);
			
			rightFallPoint = Math.round(thumbRight.x /this.incriment)*incriment;
			thumbRight.txt.text = timeConverter.getTag(rightFallPoint);
			infinityCheck();
			thumbRight.txt.autoSize = TextFieldAutoSize.LEFT;
			thumbLeft.txt.autoSize = TextFieldAutoSize.RIGHT;
			thumbRight.txt.wordWrap = false;
			thumbLeft.txt.wordWrap = false;
			scaleThumbRight()
			scaleThumbLeft()
		}
		private function infinityCheck ():void {
			if(thumbRight.txt.text == "Forever") {
				thumbRight.infinity.visible = true;
				thumbRight.txt.visible = false;
			} else {
				thumbRight.infinity.visible = false;
				thumbRight.txt.visible = true;
			}
		}
		private function moverRight (e:MouseEvent) {
			trace(e.target.name);
			//thumbRight.txt.textColor = 0xFFFFFF;
			thumbRight.darkness.visible = true;
		}
		private function moutRight (e:MouseEvent) {
			thumbRight.darkness.visible = false;
			//thumbRight.txt.textColor = 0x000000;
		}
		private function moverLeft (e:MouseEvent) {
			//thumbLeft.txt.textColor = 0xFFFFFF;
			thumbLeft.darkness.visible = true;
		}
		private function moutLeft (e:MouseEvent) {
			thumbLeft.darkness.visible = false;
			//thumbLeft.txt.textColor = 0x000000;
		}
		
		private function md (e:MouseEvent): void {
				this.xOffset = e.target.parent.mouseX;
			if(e.target.parent.name == "thumbRight") {
				addEventListener(MouseEvent.MOUSE_MOVE, dragRight, false, 0, true);
				stage.addEventListener( MouseEvent.MOUSE_UP, muRight, false, 0, true);
			} else {
				addEventListener( Event.ENTER_FRAME, dragLeft, false, 0, true);
				stage.addEventListener( MouseEvent.MOUSE_UP, muLeft, false, 0, true);
			}
		}
		private function dragRight (e:Event = null):void {
			thumbRight.x = mouseX - xOffset;
			if(thumbRight.x > yMax) {
				thumbRight.x = yMax;
			} else if (thumbRight.x < thumbLeft.x + incriment*.6) {
				thumbRight.x = thumbLeft.x + incriment;
			}
			TweenLite.to(blueBar, .6,{ease:Quint.easeOut, width:thumbRight.x- thumbLeft.x});
			rightFallPoint = Math.round(thumbRight.x /this.incriment)*incriment;
			trace(rightFallPoint);
			thumbRight.txt.text = timeConverter.getTag(rightFallPoint);
			infinityCheck();
			scaleThumbRight()
		}
		private function scaleThumbRight():void {
			if(thumbRight.txt.visible) {
				TweenLite.to(thumbRight.mid, .2,{width:thumbRight.txt.width-12, ease:Quad.easeOut, onUpdate:scaleThumbRight2});
			} else {
				TweenLite.to(thumbRight.mid, .2,{width:thumbRight.infinity.width-4, ease:Quad.easeOut, onUpdate:scaleThumbRight2});
			}
		}
		private function scaleThumbRight2():void {
			thumbRight.txt.alpha = .6;
			TweenLite.to(thumbRight.txt, .1,{alpha:1});
			thumbRight.right.x = thumbRight.mid.width+thumbRight.mid.x;
				thumbRight.darkness.width = thumbRight.mid.width+thumbRight.right.width+thumbRight.left.width-12;
		}
		private function scaleThumbLeft():void {
				TweenLite.to(thumbLeft.mid, .2,{width:thumbLeft.txt.width-12, ease:Quad.easeOut, onUpdate:scaleThumbLeft2});
			
		}
		private function scaleThumbLeft2():void {
			thumbLeft.txt.alpha = .6;
			TweenLite.to(thumbLeft.txt, .1,{alpha:1})
			thumbLeft.mid.x = thumbLeft.right.x-thumbLeft.mid.width;
			thumbLeft.left.x = thumbLeft.mid.x-thumbLeft.left.width
				thumbLeft.darkness.width = thumbLeft.mid.width+thumbLeft.right.width+thumbLeft.left.width-12;
		}
		private function dragLeft (e:Event = null):void {
			thumbLeft.x = mouseX - xOffset;
			if(thumbLeft.x < yMin) {
				thumbLeft.x = yMin;
			} else if (thumbLeft.x > thumbRight.x - incriment*.6) {
				thumbLeft.x = thumbRight.x - incriment;
			}
			TweenLite.to(blueBar, .6,{ease:Quint.easeOut,x:thumbLeft.x, width:thumbRight.x- thumbLeft.x});
			leftFallPoint = Math.round(thumbLeft.x /this.incriment)*incriment;
			thumbLeft.txt.text = timeConverter.getTag(leftFallPoint);
			dispatchEvent( new EventWithParams("currentLeftValue", true , false, [leftFallPoint]));
			scaleThumbLeft()
		}
		private function muRight (e:MouseEvent):void {
			removeEventListener(MouseEvent.MOUSE_MOVE, dragRight, false);
			stage.removeEventListener( MouseEvent.MOUSE_UP, muRight, false);
			var fallPoint:int = Math.round(thumbRight.x /this.incriment)*incriment;
			TweenLite.to(thumbRight, .5,{x:fallPoint, ease:Quint.easeOut});
			TweenLite.to(blueBar, .6,{ease:Quint.easeOut, width:fallPoint - thumbLeft.x});
			newDate();
		}
		private function muLeft (e:MouseEvent):void {
			removeEventListener( Event.ENTER_FRAME, dragLeft, false);
			stage.removeEventListener( MouseEvent.MOUSE_UP, muLeft, false);
			var fallPoint:int = Math.round(thumbLeft.x /this.incriment)*incriment;
			TweenLite.to(thumbLeft, .5,{x:fallPoint, ease:Quint.easeOut});
			TweenLite.to(blueBar, .6,{ease:Quint.easeOut,x:fallPoint, width:thumbRight.x- fallPoint});
			newDate();
		}
		private function newDate() :void {
			var fromD:String = this.timeConverter.tagToYYYYMMDD(this.leftFallPoint);
			var toD:String = this.timeConverter.tagToYYYYMMDD(this.rightFallPoint);
			trace(fromD+","+toD);
			dispatchEvent(new EventWithParams(this.CHANGE_TIMESPAN, false, false, [fromD+","+toD]));
		}
	}
}